package com.google.code.alpaca.prototype.dao.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.google.code.alpaca.prototype.dao.MessageDAO;
import com.google.code.alpaca.prototype.model.Message;

class MessageDAOImpl implements MessageDAO {

	MessageDAOImpl() {
	}

	@Inject
	private EntityManager em;

	@Override
	public List<Message> getAll() {
		CriteriaQuery<Message> c = em.getCriteriaBuilder().createQuery(
				Message.class);
		Root<Message> r = c.from(Message.class);
		c.select(r);
		TypedQuery<Message> query = em.createQuery(c);
		List<Message> messages = query.getResultList();
		return messages;
	}

	@Override
	public void create(Message message) {
		em.persist(message);
	}

	@Override
	public void deleteById(Long id) {
		Message msg = em.find(Message.class, id);
		em.remove(msg);
	}

}
